/*
 * Option.java
 *
 * Created on May 15, 2008, 2:11 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package morseme;

/**
 *
 * @author tt
 */
import javax.microedition.lcdui.*;

public class Option extends Form implements CommandListener {

    private static String[] opts = {"On", "Off"};
    private static String[] inv_opts = {"Off", "On"};
    private static Command okCmd = new Command("OK", Command.OK, 0);
    private static Command cancelCmd = new Command("Cancel", Command.CANCEL, 2);
    private ChoiceGroup soundGroup = new ChoiceGroup("Sound", ChoiceGroup.EXCLUSIVE, opts, null);
    private ChoiceGroup vibrateGroup = new ChoiceGroup("Vibration", ChoiceGroup.EXCLUSIVE, opts, null);
    private ChoiceGroup autoGroup = new ChoiceGroup("Auto Play", ChoiceGroup.EXCLUSIVE, opts, null);
    private ChoiceGroup usrGroup = new ChoiceGroup("Username Used:",ChoiceGroup.EXCLUSIVE,inv_opts,null);
    private ChoiceGroup pwdGroup = new ChoiceGroup("Password Used:",ChoiceGroup.EXCLUSIVE,inv_opts,null);
    private TextField numAutoTF = new TextField("Number of Auto Play : ", "0", 160, TextField.DECIMAL);
    private TextField usrField = new TextField("Username: \n","",25,TextField.ANY);
    private TextField pwdField = new TextField("Password: \n","",4,TextField.DECIMAL);
    private MainMenu menu;
    public static int SOUND_SEL = 0;
    public static int VIBRATION_SEL = 0;
    public static int AUTO_SEL = 0;
    public static int NUM_AUTO_PLAY = 1;
    public static int USR_ON = 0;
    public static String USR_STRING = null;
    public static int PWD_ON = 0;
    public static String PWD_STRING = null;
    public static int USER_TYPE = 1;

    public Option(MainMenu menu) {

        super("Setting");
        this.menu = menu;

        this.addCommand(okCmd);
        this.addCommand(cancelCmd);
        this.setCommandListener(this);
        
        this.append(soundGroup);
        this.append(vibrateGroup);
        this.append(autoGroup);
        this.append(numAutoTF);
        this.append(this.usrGroup);
        this.append(this.usrField);
        this.append(this.pwdGroup);
        this.append(this.pwdField);

        SOUND_SEL = soundGroup.getSelectedIndex();
        VIBRATION_SEL = vibrateGroup.getSelectedIndex();
        AUTO_SEL = autoGroup.getSelectedIndex();
        NUM_AUTO_PLAY = Integer.parseInt(numAutoTF.getString());
        USR_ON = this.usrGroup.getSelectedIndex();
        PWD_ON = this.pwdGroup.getSelectedIndex();
        USR_STRING = this.usrField.getString();
        PWD_STRING = this.pwdField.getString();

    }

    protected void refresh() {

        soundGroup.setSelectedIndex(SOUND_SEL, true);
        vibrateGroup.setSelectedIndex(VIBRATION_SEL, true);
        autoGroup.setSelectedIndex(AUTO_SEL, true);
        numAutoTF.setString(Integer.toString(NUM_AUTO_PLAY));
        this.usrGroup.setSelectedIndex(USR_ON, true);
        this.pwdGroup.setSelectedIndex(PWD_ON,true);
        this.usrField.setString(USR_STRING);
        this.pwdField.setString(PWD_STRING);
    }

    public void commandAction(Command c, Displayable d) {

        if (c == okCmd) {

            SOUND_SEL = soundGroup.getSelectedIndex();
            VIBRATION_SEL = vibrateGroup.getSelectedIndex();
            AUTO_SEL = autoGroup.getSelectedIndex();
            NUM_AUTO_PLAY = Integer.parseInt(numAutoTF.getString());
            USR_ON = this.usrGroup.getSelectedIndex();
            PWD_ON = this.pwdGroup.getSelectedIndex();
            USR_STRING = this.usrField.getString();
            PWD_STRING = this.pwdField.getString();
        }

        Display.getDisplay(MorseME.getMIDlet()).setCurrent(this.menu);
    }

}

